/***************************************************************************
 *   Copyright (C) 2006 by Matej Svejda                                    *
 *   mata@aw-modell.at                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "destickui.h"
#include "destickui.moc"

DestickUI::DestickUI(KConfig *aConfig, QWidget *parent, const char *name) :
	QWidget(parent, name)
{
	config = aConfig;

	config->setGroup("Metronome");
	int beat = config->readNumEntry("Beat", 0);
	int bpm = config->readNumEntry("BPM", 100);
	float volume = static_cast<float>(config->readDoubleNumEntry("Volume", 0.8));
	int accent = config->readNumEntry("Accent", 0);
	bool bpmToolTipVisible = config->readBoolEntry("BPMToolTip", true);
	visualizationVisible = config->readBoolEntry("Visualization", true);
	
	int minBPM = config->readNumEntry("MinBPM", 30);
	int maxBPM = config->readNumEntry("MaxBPM", 200);
	

	// Load the Metronome.
	// The Metronome does alsmost everything...
	metronome = new Metronome(this);
	metronome->setVisualizationVisibility(visualizationVisible);
	
	// Set default beat.
	metronome->setBeat(beat);
	metronome->setBPM(bpm);
	
	connect(metronome, SIGNAL(visualizationVisibilityChanged(bool)), this, SLOT(updateVisualizationVisibility(bool)));

	QLabel *bpmSliderLabel = new QLabel(i18n("BPM:"), this);

	// Load slider to set BPM (beats per minute)
	bpmSlider = new BPMSlider(this);
	bpmSlider->setRange(minBPM, maxBPM);
	bpmSlider->setBPM(bpm);
	connect(bpmSlider, SIGNAL(bpmChanged(int)), metronome, SLOT(setBPM(int)));
	connect(bpmSlider, SIGNAL(returnPressed()), metronome, SLOT(start()));
	
	// Load combobox for select the beat type.
	QLabel *beatSelectLabel = new QLabel(i18n("Beat Type:"), this);
	beatSelect = new BeatSelect(beat, this);
	connect(beatSelect, SIGNAL(beatChanged(int)), metronome, SLOT(setBeat(int)));

	//Load Volume slider.
	QLabel *volumeSliderLabel = new QLabel(i18n("Volume:"), this);
	volumeSlider = new VolumeSlider(this);
	connect(volumeSlider, SIGNAL(volumeChanged(float)), metronome, SIGNAL(volumeChanged(float)));
	volumeSlider->setVolume(volume);
	
	// Load combobox to set the accentuation.
	QLabel *accentSelectLabel = new QLabel(i18n("Accentuation:"), this);
	accentSelect = new AccentSelect(accent, this);
	connect(accentSelect, SIGNAL(accentChanged(int)), metronome, SLOT(setAccent(int)));
	accentSelect->calculateAccentuated(accent); // Workaround because at construct signals and slots aren't connected.
	
	// Start/Stop button.
	ToggleButton *controlButton = new ToggleButton(this);
	connect(controlButton, SIGNAL(start()), metronome, SLOT(start()));
	connect(controlButton, SIGNAL(stop()), metronome, SLOT(stop()));
	connect(bpmSlider, SIGNAL(returnPressed()), controlButton, SLOT(activate()));
	
	controlButton->setFocus();
	
	if(bpmToolTipVisible) {
		// Tooltip that gives a hint for the special function of the lineinput.
		bpmToolTip = new BPMToolTip(this);
		bpmToolTip->setGeometry(0, 50, bpmToolTip->width(), bpmToolTip->height());
		connect(bpmToolTip, SIGNAL(clicked()), this, SLOT(removeBPMToolTip()));
		connect(bpmSlider, SIGNAL(bpmEditClicked()), this, SLOT(removeBPMToolTip()));
	}
	
	QPushButton *configButton = new QPushButton(this);
	configButton->setPixmap(KGlobal::iconLoader()->loadIcon("advancedsettings", KIcon::Small));
	configButton->setMaximumWidth(35);	
		
	configContextMenu = new QPopupMenu(this);
	configContextMenu->setCheckable(true);
	visualizationVisibilityIndex = configContextMenu->insertItem(i18n("Visualization"), this, SLOT(toggleVisualizationVisibility()));
	if(visualizationVisible) {
		configContextMenu->setItemChecked(visualizationVisibilityIndex, true);
	}
	
	configButton->setPopup(configContextMenu);
	
	layout = new QGridLayout(this);
	
	QHBoxLayout *bpmSliderLayout = new QHBoxLayout(this);
	bpmSliderLayout->addWidget(bpmSliderLabel);
	bpmSliderLayout->addSpacing(5);
	bpmSliderLayout->addWidget(bpmSlider);
	
	QHBoxLayout *buttonLayout = new QHBoxLayout(this);
	buttonLayout->addWidget(controlButton);
	buttonLayout->addSpacing(10);
	buttonLayout->addWidget(configButton);
	
	QGridLayout *settingsLayout = new QGridLayout(this);
	settingsLayout->addWidget(beatSelectLabel, 0, 0);
	settingsLayout->addWidget(beatSelect, 0, 2);
	settingsLayout->addWidget(volumeSliderLabel, 2, 0);
	settingsLayout->addWidget(volumeSlider, 2, 2);
	settingsLayout->addWidget(accentSelectLabel, 4, 0);
	settingsLayout->addWidget(accentSelect, 4, 2);
	settingsLayout->addMultiCellLayout(buttonLayout, 6, 8, 0, 2);
	settingsLayout->setColSpacing(1, 5);
	settingsLayout->setRowSpacing(1, 5);
	settingsLayout->setRowSpacing(3, 20);
	settingsLayout->setRowSpacing(5, 20);

	layout->addMultiCellWidget(metronome, 1, 1, 2, 4, Qt::AlignCenter);
	layout->addMultiCellLayout(bpmSliderLayout, 2, 2, 2, 4, Qt::AlignTop);
	if(bpmToolTipVisible) {
		layout->addMultiCellWidget(bpmToolTip, 2, 2, 2, 4, Qt::AlignBottom + Qt::AlignRight);
	}
	layout->addLayout(settingsLayout, 3, 3);
	layout->setColSpacing(1, 10);
	layout->setColSpacing(2, 100);
	layout->setColSpacing(4, 100);
	layout->setColSpacing(5, 10);
	layout->setRowSpacing(0, 10);
	layout->setRowSpacing(1, 25);
	layout->setRowSpacing(2, 50);
	layout->setRowSpacing(4, 10);
}

void DestickUI::removeBPMToolTip()
{
	delete(bpmToolTip);
	disconnect(bpmSlider, SIGNAL(bpmEditClicked()), 0, 0);
}

DestickUI::~DestickUI()
{
	config->writeEntry("BPM", bpmSlider->getBPM());
	config->writeEntry("Beat", beatSelect->getBeat());
	config->writeEntry("Accent", accentSelect->getAccent());
	config->writeEntry("Volume", volumeSlider->getVolume());
	config->writeEntry("BPMToolTip", false);
	config->writeEntry("Visualization", visualizationVisible);
}

void  DestickUI::updateVisualizationVisibility(bool visible)
{
	visualizationVisible = visible;
	configContextMenu->setItemChecked(visualizationVisibilityIndex, visualizationVisible);
}

void DestickUI::toggleVisualizationVisibility()
{
	updateVisualizationVisibility(!visualizationVisible);
	metronome->setVisualizationVisibility(visualizationVisible);
}
